/**
 * Created by root on 2017/7/18.
 */
window.onload=function() {
    var user_text = document.getElementById("user_text");
    var user_mail = document.getElementById("user_mail");
    var pass_text = document.getElementById("pass_text");
    var pass_again = document.getElementById("pass_again");
    var iden_text = document.getElementById("iden_text");
    var login_btn = document.getElementById("login_button");
    var iden_btn=document.getElementById("iden_btn");
    var flag = true;

    user_text.onblur = function () {
        if (user_text.value == "") {
            document.getElementById("s4").innerText = " 用户名不能为空！";
            this.focus();
            flag = false;
        }
        else {
            document.getElementById("s4").innerText = "";
        }
    }
    user_mail.onblur = function () {
        var myReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
        if (!myReg.test(user_mail.value)) {
            document.getElementById("s1").innerText = " 邮箱格式错误！";
            user_mail.value = "";
            user_mail.focus();
            flag = false;
        }
        else {
            document.getElementById("s1").innerText = "";
        }
    }
    pass_text.onblur=function(){
        if(pass_text.value=="")
        {
            document.getElementById("s2").innerText=" 密码不能为空！";
            this.focus();
            flag=false;
        }
        else{
            document.getElementById("s2").innerText="";
        }
    }
    pass_again.onblur = function () {
        var p1 = document.getElementById("pass_text");
        var p2 = document.getElementById("pass_again");
        if (p1.value != p2.value) {
            document.getElementById("s3").innerText = " 两次密码输入不一致！";
            p2.value = "";
            p1.value="";
            p1.focus();
            flag = false;
        }
    }
    //获取验证码
    iden_btn.onclick = function () {
        var request=new XMLHttpRequest();
        request.open("POST","i/noteuser/noteUser/sendCodeRegister",true);
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var message="userName="+user_text+"&password="+pass_text+"&mail="+user_mail;
        request.onreadystatechange=function(){
            if(request.readyState==4)
            {
                if(request.status==200)
                {
                    var data=JSON.parse(request.responseText);
                    if(data.result==1)
                    {
                        alert("发送成功！");
                    }
                }
                else{
                    alert("发送失败 "+request.status);
                }
            }
        }
        request.send(message);
    }
    //注册
    login_btn.onclick=function(){
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("POST","i/noteuser/noteUser/saveUser",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var message="userName="+user_text+"&password="+pass_text+"&code="+iden_text+"&mail="+user_mail;
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200)
                {
                    var data=JSON.parse(xmlhttp.responseText);
                    if(data.result==1)
                    {
                        alert("注册成功");
                        window.location.href="Start_lode.jsp";
                    }
                }
                else{
                    alert("请求失败"+xmlhttp.status);
                }
            }
        }
    }
}